package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.mapper.PInformationMapper;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.mapper.TeamMapper;
import com.team11.server.pojo.SInformation;
import com.team11.server.pojo.Team;
import com.team11.server.pojo.dto.RespPageBean;
import com.team11.server.pojo.dto.SKStudentRecord;
import com.team11.server.pojo.dto.vo.SkStudentRecordVo;
import com.team11.server.pojo.student.P_G_M_Kc_information;
import com.team11.server.pojo.student.P_S_Kc_information;
import com.team11.server.pojo.student.StudentGroupScore;
import com.team11.server.pojo.student.StudentInformation;
import com.team11.server.service.ISInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pjc
 * @since 2021-06-15
 */
@Service
public class SInformationServiceImpl extends ServiceImpl<SInformationMapper, SInformation> implements ISInformationService {

    @Resource
    private SInformationMapper sInformationMapper;

    @Resource
    private PInformationMapper pInformationMapper;

    @Resource
    private TeamMapper teamMapper;

    /**
     * 获取上课学生信息
     *
     * @param currentPage
     * @param size
     * @param kc_s_id
     * @param skStudentRecordVo
     * @return
     */
    @Override
    public RespPageBean getStudentByKcSID(Integer currentPage, Integer size, Integer kc_s_id,
                                          SkStudentRecordVo skStudentRecordVo) {
        Page<SKStudentRecord> page = new Page<>(currentPage, size);
        IPage<SKStudentRecord> studentByKcSID = sInformationMapper.getStudentByKcSID(page, kc_s_id, skStudentRecordVo);
        RespPageBean respPageBean = new RespPageBean(studentByKcSID.getTotal(), studentByKcSID.getRecords());
        return respPageBean;
    }

    /**
     * 根据小组id获取小组成员和小组id
     *
     * @param pId
     * @return
     */
    @Override
    public List getStudentByPId(List<Integer> pId) {
        List list1= new ArrayList();
        for (int i = 0; i < pId.size(); i++) {
//            List<Map<String, Object>> list = teamMapper.selectMaps(new QueryWrapper<Team>()
//                    .eq("p_id", pId.get(i)).select("sid", "p_id"));
//            sInformationMapper.selectMaps(new QueryWrapper<>())
//            list1.add(list);
            List<Map<String, Object>> list= sInformationMapper.getStudentByPId( pId.get(i));
            list1.add(list);
        }
        return list1;
    }

    @Override
    public Map getStudent(String rId) {
        Map<String,Object> map =sInformationMapper.getStudent(rId);
        return map;
    }

    //杜康
    /**
     *通过学生学号来查询个人详细信息
     * @param sNumber 学生学号
     */
    public StudentInformation getDetails(String sNumber){
        return sInformationMapper.getStudentsBysId(sNumber);
    }
    /**
     *更新邮箱地址
     * @param sNumber 学生学号
     * @param sEmail 邮箱地址
     */
    public String updateEmail(String sNumber,String sEmail){
        String p="\\w+[@]qq\\.com";
        if (sEmail.matches(p)) {
            Integer count = sInformationMapper.getCountByEmailForUpdate(sNumber, sEmail);
            if (count > 0) {
                return "该邮箱地址已存在！";
            }
            Integer row = sInformationMapper.updateEmail(sNumber, sEmail);
            if (row > 0) {
                return "邮箱地址更新成功！";
            } else {
                return "邮箱地址更新失败！";
            }
        } else {
            return "请输入正确的邮箱！";
        }
    }

    /**
     * 更新手机号码
     * @param sNumber 学生学号
     * @param sPhone 手机号码
     */
    public String updatePhone(String sNumber,String sPhone){
        String p="^[1][3456789][0-9]{9}$";
        if (sPhone.matches(p)) {
            Integer count = sInformationMapper.getCountByPhoneForUpdate(sNumber, sPhone);
            if (count > 0) {
                return "该手机号码已存在！";
            }
            Integer row = sInformationMapper.updatePhone(sNumber, sPhone);
            if (row > 0) {
                return "手机号码更新成功！";
            } else {
                return "手机号码更新失败！";
            }
        } else {
            return "请输入正确的手机号！";
        }
    }

    /**
     * 获取项目组成员信息
     * @param sNumber 学生学号
     */
    public Object getGroupList(String sNumber){
        Integer s=sInformationMapper.getSidById(sNumber);
        if (s==null){
            return "该学生不存在！";
        }
        List<Integer> p=sInformationMapper.getPidBysId(s);
        List<P_S_Kc_information> p_s_kc_information=new ArrayList<>();
        for (Integer integer : p) {
            List<P_S_Kc_information> p_s_kc_informationList=pInformationMapper.getProjectGroupById(integer);
            for (P_S_Kc_information pSKcInformation : p_s_kc_informationList) {
                String sId=pInformationMapper.getsIdBysid(pSKcInformation.getPSetLeader());
                P_S_Kc_information p_s_kc_information1 = new P_S_Kc_information();
                if (sId.equals(pSKcInformation.getSNumber())) {
                    p_s_kc_information1.setIsLeader(1);
                }else {
                    p_s_kc_information1.setIsLeader(0);
                }
                p_s_kc_information1.setPId(pSKcInformation.getPId());
                p_s_kc_information1.setKcName(pSKcInformation.getKcName());
                p_s_kc_information1.setPName(pSKcInformation.getPName());
                p_s_kc_information1.setSName(pSKcInformation.getSName());
                p_s_kc_information1.setSProjectRole(pSKcInformation.getSProjectRole());
                p_s_kc_information1.setPSetLeader(pSKcInformation.getPSetLeader());
                p_s_kc_information1.setKcPeriod(pSKcInformation.getKcPeriod());
                p_s_kc_information1.setKcLargeClass(pSKcInformation.getKcLargeClass());
                p_s_kc_information1.setKcSelfStudy(pSKcInformation.getKcSelfStudy());
                p_s_kc_information1.setSPhone(pSKcInformation.getSPhone());
                p_s_kc_information.add(p_s_kc_information1);
            }
        }
        return p_s_kc_information;
    }

    /**
     * 获取项目组得分和个人得分情况
     * @param sNumber 学生学号
     */
    public Object getScoreList(String sNumber){
        Integer s=sInformationMapper.getSidById(sNumber);
        if (s==null){
            return "该学生不存在！";
        }
        List<Integer> p=sInformationMapper.getPidBysId(s);
        List<P_G_M_Kc_information> pGMKcInformation=new ArrayList<>();
        for (Integer integer : p) {
            List<P_G_M_Kc_information> p_g_m_kc_informationList= pInformationMapper.getProjectGroupScore(integer, s);
            for (P_G_M_Kc_information pGMKcInformation1:p_g_m_kc_informationList){
                P_G_M_Kc_information pGMKcInformation2=new P_G_M_Kc_information();
                pGMKcInformation2.setPId(pGMKcInformation1.getPId());
                pGMKcInformation2.setKcId(pGMKcInformation1.getKcId());
                pGMKcInformation2.setKcName(pGMKcInformation1.getKcName());
                pGMKcInformation2.setPName(pGMKcInformation1.getPName());
                pGMKcInformation2.setMScore(pGMKcInformation1.getMScore());
                pGMKcInformation2.setGScore(pGMKcInformation1.getGScore());
                pGMKcInformation.add(pGMKcInformation2);
            }
        }
        for (Integer i=0;i<pGMKcInformation.size();i++){
            List<StudentGroupScore> studentGroupScores=pInformationMapper.getRowNumListByKcName(pGMKcInformation.get(i).getKcName());
            for(Integer j=0;j<studentGroupScores.size();j++){
                if(studentGroupScores.get(j).getPId().equals(pGMKcInformation.get(i).getPId())){
                    pGMKcInformation.get(i).setRowNum(studentGroupScores.get(j).getRowNum());
                }
            }
        }
        return pGMKcInformation;
    }

    /**
     * 获取项目组排名榜信息
     * @param kcName 项目名称
     */
    public List<StudentGroupScore> getRowNumList(String kcName){
        return pInformationMapper.getRowNumListByKcName(kcName);
    }
}
